package com.atguigu.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;
import com.atguigu.entity.RolePermission;
import com.atguigu.service.PermissionService;
import com.atguigu.util.PermissionHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {

    @Autowired
    PermissionDao permissionDao;

    @Autowired
    RolePermissionDao rolePermissionDao;
    @Override
    public BaseDao<Permission> getEntityDao() {
        return permissionDao;
    }

    @Override
    public List<Map<String, Object>> findPermissionByRoleId(Long id) {
        //1.查询所有许可（权限）节点
        List<Permission> allPermission = permissionDao.findAll();
        //2.哪些节点需要进行勾选？ 查询当前的角色所对应的许可。
        List<Map<String, Object>> zNodes = new ArrayList<>();
        List<Long> permissionIdList = rolePermissionDao.findPermissionIdListByRoleId(id);

        for (Permission permission : allPermission) {
            Map<String, Object> map = new HashMap<>();
            //无需设置 open=true  因为对整棵树进行设置
            //{ id:21, pId:2, name:"随意勾选 2-1"},
            map.put("id", permission.getId());
            map.put("pId",permission.getParentId());
            map.put("name",permission.getName());
            //是否checked表示复选框是否打钩，表示用于的许可，进行回显
            if (permissionIdList.contains(permission.getId())) {
                map.put("checked",true);
            }
            zNodes.add(map);
        }

        return zNodes;
    }

    @Override
    public void assignPermission(Long roleId, Long[] permissionIds) {
        //1.先删除旧的关系数据
        rolePermissionDao.delete(roleId);
        //2.保存新的勾选的关系数据
        if (permissionIds != null && permissionIds.length > 0) {//注意，整棵树一个对勾都没勾选，数组是空值
            //批量保存
            ArrayList<RolePermission> list = new ArrayList<>();
            for (Long permissionId : permissionIds) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setPermissionId(permissionId);
                rolePermission.setRoleId(roleId);
                list.add(rolePermission);
            }
            rolePermissionDao.insertBatch(list);
        }
    }

    @Override
    public List<Permission> findMenuPermissionByAdminId(Long adminId) {
        List<Permission> list = null;
        //如果是系统（超级）管理员，默认id=1，直接查所有权限，无序条件关联查询
        if (adminId.intValue() == 1) { //超级管理员
            list = permissionDao.findAll(); //可以不用查询type=2的permission，是按钮
        }else {
            //如果是普通管理员，就需要根据用户获取角色，在获取相应的权限。需要进行条件，关联查询
            list = permissionDao.findPermissionListByAdminId(adminId);//按照type=1进行查询，只查询菜单，不查询按钮
        }


        List<Permission> listPermission = PermissionHelper.bulid(list);
        return listPermission;//组装父子关系才能返回
    }

    @Override
    public List<Permission> findAllMenu() {
        //全部权限列表
        List<Permission> permissionList = permissionDao.findAll();
        if(CollectionUtils.isEmpty(permissionList)) return null;

        //构建树形数据,总共三级
        //把权限数据构建成树形结构数据
        List<Permission> result = PermissionHelper.bulid(permissionList);
        return result;
    }

    @Override
    public List<String> findAllCode() {
        return permissionDao.findAllCode();
    }

    @Override
    public List<String> findCodeByAdminId(Long adminId) {
        return permissionDao.findCodeByAdminId(adminId);
    }
}
